// pages/Search/index/index.js
var app = getApp();
var ikz = require('../../Common/tools.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choosedis:'none',
    town_info:'',//所有镇的信息
    type_info:'',//所有信息分类
    now_town:'',//当前选中城镇信息
    input_value:'',//input框的值
   
  },
  //选择地址
  chosAddress:function(e){
    this.setData({
      choosedis: 'block',
    })
  },
  //关闭选择地址
  shutAddress: function (e) {
    this.setData({
      choosedis: 'none',
    })
  },
  //获取input框的值
  bindKeyInput: function (e) {
    this.setData({
      input_value: e.detail.value,
    })
  },
  //选择城镇
  chooseTown: function (e) {
    var tid = e.target.dataset.tid;
    var town_info = this.data.town_info;
    //获取当前所选城镇
    for (var i = 0; i < town_info.length; i++){
      if (tid == town_info[i]['town_id']){
        var now_town = town_info[i];
      }
    }
    this.setData({
      now_town: now_town,
      choosedis: 'none',
    })
  },
  
  //选择分类跳转信息页
  chooseType: function (e) {
    var tyid = e.currentTarget.dataset.tyid;
    console.log(e)
    var twid = this.data.now_town.town_id;
    wx.navigateTo({
      url: '../list/list?tyid=' + tyid +'&twid=' + twid,
    })
  },
  //点击搜索跳转信息页
  searchAll: function (e) {
    var input_value = this.data.input_value;
    var twid = this.data.now_town.town_id;
    if (input_value == ''){
      wx.showToast({
        title: '内容不能为空',
        duration: 2000
      });
      return false;
    }
    wx.navigateTo({
      url: '../list/list?input_value=' + input_value + '&twid=' + twid,
    })
  },

  //跳转添加信息页
  toAdd:function(e){
    wx.navigateTo({
      url: '../addinfo/addinfo',
    })
  },

  tomore:function(){
    console.log('点击更多');
    var that = this;
    // ikz.over(that);
    var path = 'Search/index';
    ikz.request(path, {}, false, (res) => {
      console.log(res)
      if (res.data.status == 'success') {
        var now_town = res.data.town_info[0];
        console.log(res.data.type_info);
        that.setData({
          town_info: res.data.town_info,
          type_info: res.data.type_info,
          now_town: now_town,
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    ikz.over(that);
    var path = 'Search/index';
    ikz.request(path, {}, false, (res) => {
      console.log(res)
      if (res.data.status == 'success') {
        var now_town = res.data.town_info[0];

        console.log(res.data.type_info);
        var arr=res.data.type_info.splice(0,7);
        console.log(arr)
        // for(var i=0;i<res.data.type_info.length;i++){
        //   console.log(res.data.type_info[i])
        // }
      
        that.setData({
          town_info: res.data.town_info,
          type_info: arr,
          now_town: now_town,
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    ikz.hide(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})